<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true">添加分类</el-button>
      </div>
      <el-table :data="artlist" border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>

        <el-table-column label="操作" width="200px">
          <template v-slot="{ row }" label="操作" width="200px">
            <el-button type="primary" size="mini" @click="editupdart(row)">修改</el-button>
            <el-button type="danger" size="mini" @click="delete_art(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加的表单 -->
    <el-dialog
      :close-on-click-modal="false"
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="30%"
      @close="toreset"
    >
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="toaddart">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改的表单 -->
    <el-dialog
      :close-on-click-modal="false"
      title="修改文章分类"
      :visible.sync="editVisible"
      width="30%"
      @close="toeditreset"
    >
      <!-- 修改的表单 -->
      <el-form :model="editForm" :rules="editRules" ref="editRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="toupdart">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { artAPI, artaddAPI, artupdAPI, artdadAPI } from '@/api/cate.js'
export default {
  name: 'ArtCate',
  data() {
    return {
      artlist: [],
      // 设置对话框一开始的状态
      dialogVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // 修改表单的数据对象
      editForm: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },
      editRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      editVisible: false
    }
  },
  created() {
    this.toartlist()
  },
  methods: {
    async toartlist() {
      const { data: res } = await artAPI()
      console.log(res)
      if (res.code === 1) {
        this.$message.error(res.$message)
      } else {
        this.$message.success(res.$message)
        this.artlist = res.data
      }
    },
    // 重置
    toreset() {
      this.$refs.addRef.resetFields()
    },

    // 添加
    async toaddart() {
      this.dialogVisible = false
      const { data: res } = await artaddAPI(this.addForm)
      if (res.code === 1) {
        this.$message.error(res.message)
      } else {
        this.$message.success(res.message)
        this.toartlist()
      }
    },
    //  修改
    editupdart(index) {
      console.log(index)
      if (index.id === 1 || index.id === 2) {
        return this.$message.error('管理员不允许更新 id 为 1（最新） 和 2（科技） 的分类！')
      }
      this.editVisible = true
      this.editForm = { ...index }
    },
    // 修改重置
    toeditreset() {
      this.$refs.editRef.resetFields()
    },
    // 点击修改确定后操作
    toupdart() {
      this.$refs.editRef.validate(async valid => {
        if (!valid) return
        this.editVisible = false
        const { data: res } = await artupdAPI(this.editForm)
        if (res.code === 1) {
          this.$message.error(res.message)
        } else {
          this.$message.success(res.message)
          this.toartlist()
        }
      })
    },
    // 删除
    async delete_art(index) {
      if (index === 1 || index === 2) {
        return this.$message.error('管理员不允许删除 id 为 1（最新） 和 2（科技） 的分类！')
      } else {
        const ress = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (ress === 'cancel') {
          this.$message.info('取消删除成功')
        } else {
          this.$message.success('删除成功')
          const { data: res } = await artdadAPI(index)
          if (res.code === 1) {
            this.$message.error(res.message)
          } else {
            this.$message.success(res.message)
            this.toartlist()
          }
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
